<template>
  <div class="carhub">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>车辆管理</el-breadcrumb-item>
      <el-breadcrumb-item>车辆信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区 -->
    <el-card class="box_card">
      <!-- 顶部搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            class="input-with-select"
            placeholder="车牌号搜索"
            v-model.trim="car_number_search"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="carnumber()"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 状态查询 -->
        <el-col :span="8">
          <el-select
            v-model="stateval"
            clearable
            placeholder="车辆状态"
            @change="newselect()"
          >
            <el-option
              v-for="item in car_state"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <!-- 车辆信息列表区 -->
      <el-table :data="car_info" border stripe class="tabel_box">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="车牌号" prop="car_number"></el-table-column>
        <el-table-column label="车牌图片" prop="car_img_url">
          <template slot-scope="scope">
            <el-image
              :src="scope.row.car_img_url"
              :preview-src-list="car_img_list"
              class="car_img"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="车牌颜色" prop="car_color"></el-table-column>
        <el-table-column label="车辆进入时间" prop="car_into_time">
        </el-table-column>
        <el-table-column label="车辆离开时间" prop="car_leave_time">
          <template slot-scope="scope">
            <span>{{
              scope.row.car_leave_time !== "None"
                ? scope.row.car_leave_time
                : "未出库"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车辆在库状态" prop="car_state">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="
                scope.row.car_state.toLowerCase() === 'true' ? '在库' : '不在库'
              "
              placement="right"
            >
              <el-switch
                :value="scope.row.car_state.toLowerCase() === 'true'"
              ></el-switch>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="支付金额" prop="money">
          <template slot-scope="scope">
            <span>{{
              scope.row.money ? scope.row.money + "元" : 0 + "元"
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <div class="page_box" v-if="ispage">
        <el-pagination
          :background="true"
          :page-count="pager_count"
          @current-change="handleCurrentChange"
          :layout="col"
          :total="count_total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      car_info: [],
      car_img_list: [], //汽车图片列表
      car_number_search: null, //车牌号搜索
      pager_count: 1, // 当前页码
      count_total: 0, // 总页数
      col: "prev, pager, next, jumper,slot", //开启的配置
      ispage: true, // 是否显示分页
      car_state: [
        {
          value: "True",
          label: "在库",
        },
        {
          value: "False",
          label: "离库",
        },
      ], // 状态选择
      stateval: "", //状态值
    };
  },
  mounted() {
    // 加载立马获取车辆信息列表
    this.getCarInfo();
  },
  watch: {
    count_total(newVal) {
      if (newVal === 10) {
        this.ispage = false;
      }
    },
  },
  methods: {
    // 封装获取信息列表
    funcarinfo(parameter) {
      this.$getapi("/car_info/get", parameter, "json")
        .then((res) => {
          if (res.code === 200) {
            this.car_info = res.data;
            this.count_total = res.count * 10;
            this.addcarimg()//添加图片路径到图片列表
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 获取车辆信息列表
    getCarInfo() {
      let par = {
        page: this.pager_count,
      };
      this.funcarinfo(par);
    },
    // 车牌号搜索
    carnumber() {
      let par = {
        car_number: this.car_number_search,
      };

      this.funcarinfo(par);
    },
    // 车辆状态搜索
    newselect() {
      let par = {
        car_state: this.stateval,
      };
      this.funcarinfo(par);
      console.log(this.stateval);
    },
    // 跳转
    handleCurrentChange(val) {
      this.pager_count = val;
      this.getCarInfo();
    },
    // 添加汽车图片到图片列表
    addcarimg() {
      let list_car=[]
      for (let i = 0; i < this.car_info.length; i++) {
        list_car.push(this.car_info[i].car_img_url)
      }
      this.car_img_list = list_car
    },
  },
};
</script>

<style lang="less" scoped>
.carhub {
  .box_card {
    margin-top: 20px;
    .tabel_box {
      margin-top: 10px;
      .car_img {
        width: 100px;
        height: 50px;
        cursor: pointer;
      }
    }
    .page_box {
      margin-top: 20px;
    }
  }
}
</style>